package client.universe;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.vecmath.Quat4d;
import javax.vecmath.Vector3d;

import server.universe.*;
import client.object.CObject3DClient;
import client.object.CObject3DClientImpl;
import client.rmiManager.RMIManager;

public class CSharedUniverseImpl implements CSharedUniverse {

	private Map<String, CObject3DClient> objects;
	private PSharedUniverse presentation;
	
	public CSharedUniverseImpl() {
		objects = new HashMap<String, CObject3DClient>();
		presentation = new PSharedUniverseImpl();
		
		UniverseFacadeRemote universeFacade = RMIManager.rmiManagerInstance().getUniverseFacade();
		Collection<String> objects;
		try {
			objects = universeFacade.getAllObjects();
			for(String objName : objects) {
				//CObject3D obj = RMIManager.rmiManagerInstance().getCObject3D(objName);
				if(objName!=null) {
					CObject3DClient objClient = new CObject3DClientImpl(objName);
					this.objects.put(objName, objClient);
					presentation.addObject(objClient.getPresentation());
				}
			}
			
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public void objectUpdateTransform(String objectName, Vector3d position, Quat4d orientation, String geometry) {
		if (objects.containsKey(objectName)) {
			objects.get(objectName).updateTransform(position, orientation, geometry);
		}
	}

}
